package com.Ling.Thread.sync;

/**
 * File Name: ThreadMethod
 * Date: 2020-06-24 15:38
 * Author: liangling
 * Description
 */
public class ThreadMethod {
    public static void main(String[] args) {
        threadPriority();
    }

    private static void threadName(){
        //线程的命名
        //1.实例化一个线程对象
//        Thread t = new Thread();
//        t.setName("custom");

        //2.实例化对象的同时，通过构造方法
        Thread thread = new Thread(()->{},"custom2");

        MyThread2 thread2 = new MyThread2("custom3");

        System.out.println(thread.getName());
        System.out.println(thread2.getName());
    }


    //设置线程优先级
    private static void threadPriority(){
        //设置线程的优先级，只是修改这个线程可以去抢到CPU时间片的概率
        //并不是优先级高的线程一定能抢到CPU时间片
        //优先级的设置，是一个[0,10]整数，默认5
        Runnable r = () ->{
          for(int i=0;i<100;i++){
              System.out.println(Thread.currentThread().getName()+":"+i);
          }  
        };

        Thread t1 = new Thread(r,"Thread-1");
        Thread t2 = new Thread(r,   "Thread-2");
        //设置优先级
        //设置优先级必须要放到这个线程开始执行之前
        t1.setPriority(10);
        t2.setPriority(1);
        t1.start();
        t2.start();
    }



    //线程的休眠
    private void threadSleep(){
        MyThread2 myThread2 = new MyThread2();
        myThread2.start();
    }

}

class MyThread2 extends Thread{
    public MyThread2() {
    }

    public MyThread2(String name){
        this.setName(name);
    }


    @Override
    public void run() {
        for(int i=0;i<10;i++){
            System.out.println(i);
            try {
                //线程休眠,单位毫秒
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
